function wxpay(app, orderId, typeid = 0, relaunchUrl = '/pages/home/index?isSatrtup=2') {
  wx.request({
    url: app.globalData.host + '/api/wechat/pay',
    data: {
      uid: app.globalData.userInfo.Uid,
      storeId: app.globalData.storeId,
      oid: orderId
    },
    success: function (res) {
      if (res.data.Code == 0) {
        // 发起支付
        wx.requestPayment({
          timeStamp: res.data.Data.timeStamp,
          nonceStr: res.data.Data.nonceStr,
          package: 'prepay_id=' + res.data.Data.prepayId,
          signType: 'MD5',
          paySign: res.data.Data.paySign,
          fail: function (err) {
            if (typeid == 2) {
              wx.reLaunch({url: relaunchUrl})
            }
          },
          success: function () {
            wx.showToast({title: '支付成功'})
            if (typeid == 1) {
              return;
            } else if (typeid == 2) {
              wx.reLaunch({url: relaunchUrl});
              return;
            }
            getMyStaffIdByUid(app);            
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false,
        })
      }
    }
  })
}


// 外卖支付
function wxpayWm(app, orderId, relaunchUrl) {
  wx.request({
    url: app.globalData.host + '/api/wechat/JJKOpenPay',
    data: {
      uid: app.globalData.userInfo.Uid,
      storeId: app.globalData.storeId,
      oid: orderId
    },
    success: function (res) {
      if (res.data.Code == 0) {
        // 发起支付
        wx.requestPayment({
          timeStamp: res.data.Data.timeStamp,
          nonceStr: res.data.Data.nonceStr,
          package: 'prepay_id=' + res.data.Data.prepayId,
          signType: 'MD5',
          paySign: res.data.Data.paySign,
          fail: function (err) {
            wx.reLaunch({url: relaunchUrl})
          },
          success: function () {
            wx.showToast({title: '支付成功'})
            wx.reLaunch({url: relaunchUrl})
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false,
        })
        return;
      }
    }
  })
}
// 现金升级支付/9.9产品押金支付/玖玖卡充值/传单费用充值
function wxcashpay(app, orderId, redirectUrl, uid = '') {
  let param = {
    storeId: app.globalData.storeId,
    oid: orderId
  }
  if (uid) {
    param = {
      uid: uid,
      storeId: app.globalData.storeId,
      oid: orderId
    }
  }
  wx.request({
    url: app.globalData.host + '/api/WeChat/StaffOrderPay',
    data: param,
    method: 'POST',
    success: function (res) {
      if (res.data.Code == 0) {
        //发起支付
        wx.requestPayment({
          timeStamp: res.data.Data.timeStamp,
          nonceStr: res.data.Data.nonceStr,
          package: 'prepay_id=' + res.data.Data.prepayId,
          signType: 'MD5',
          paySign: res.data.Data.paySign,
          success: function (res) {
            wx.showToast({
              title: '支付成功'
            })
            setTimeout(function () {
              wx.reLaunch({
                url: redirectUrl,
              })
            }, 1000)
          },
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false
        })
        return;
      }
    }
  })
}

//新用户创建订单支付
function createStaffOrder(app, shareStaffId, versionid) {
  wx.showLoading({
    title: '加载中',
    mask: true,
  })
  let cardId = wx.getStorageSync('cardId');
  wx.request({
    url: app.globalData.host + '/api/Order/SubmitCreateStaffOrder',
    data: {
      uid: app.globalData.userInfo.Uid,
      shareStaffId: shareStaffId,
      industryCode: versionid
    },
    method: 'POST',
    success: function (res) {
      wx.hideLoading();
      if (res.data.Code == 0) {
        if (res.data.Data.trade_state == "SUCCESS") {
          wx.showToast({title: '创建成功'})
          wx.redirectTo({
            url: '/pages/home/index?id=' + cardId,
          })
        } else {
          wxpay(app, res.data.Data);
        }
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false,
        })
      }
    },
    fail: function (err) {
      wx.showModal({
        title: '提示',
        content: err.data.Message,
        showCancel: false,
      })
    }
  })
}

//获取个人创建的名片
function getMyStaffIdByUid(app) {
  wx.showLoading({
    title: '创建中',
    mask: true
  })
  wx.request({
    url: app.globalData.host + '/api/Staff/GetStaffIdByUid?Uid=' + app.globalData.userInfo.Uid,
    success: function (res) {
      wx.hideLoading();
      if (res.data.Code == 0) {
        if (!res.data.Data) {
        } else {
          wx.setStorageSync("staffid", res.data.Data.Id);
          wx.reLaunch({
            url: '/pages/index/index?id=' + res.data.Data.Id,
          })
        }
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.Message,
          showCancel: false,
        })
      }
    },
    fail: function (err) {
      wx.showModal({
        title: '提示',
        content: err.data.Message,
        showCancel: false,
      })
    }
  })
}

module.exports = {
  wxpay: wxpay,
  wxpayWm: wxpayWm,
  wxcashpay: wxcashpay,
  createStaffOrder: createStaffOrder,
  getStaffIdByUid: getMyStaffIdByUid
}